package com.lianniu.web.buyerOrder.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.buyerOrder.dao.IOrderCommentDao;
import com.lianniu.web.buyerOrder.model.OrderCommentVo;
import com.lianniu.web.buyerOrder.service.IOrderCommentService;
import com.lianniu.web.buyerProduct.model.ProductAssessVo;
import com.lianniu.web.buyerProduct.service.IProductAssessService;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.util.StringUtil;
@Service
public class OrderCommentServiceImpl implements IOrderCommentService {

    private static final Logger logger = Logger.getLogger(OrderCommentServiceImpl.class);
    @Autowired
    IOrderCommentDao orderCommentDao;
    @Autowired
    private IProductAssessService productAssessService;
    
    @Override
    public BaseResult selectById(OrderCommentVo record) {
        logger.info("selectById begin.");
        logger.info("selectById Id:" +record.getId());
            if(record.getId()==null){
                return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
            }
            return BaseResult.success(orderCommentDao.selectById(record.getId())); 
    }

    @Override
    public BaseResult deleteOrdercommentBatch(String... id) {
        logger.info("deleteOrdercommentBatch begin");
        logger.info("deleteOrdercommentBatch Ids:"+id.toString());
        logger.info("deleteOrdercommentBatch: input data check begin");
         BaseResult baseResult;
         if (StringUtil.isEmpty(id.toString())) {
          baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
          return baseResult;
        }
        logger.info("deleteOrdercommentBatch: input data check end");
        return BaseResult.success(orderCommentDao.deleteOrdercommentBatch(id)); 
    }

    @Override
    @Transactional
    public BaseResult insertOrdercomment(OrderCommentVo record) {
       logger.info("insertOrdercomment begin");
       logger.info("insertOrdercomment: input data check begin");
       logger.info("insertOrdercomment record:" +JSON.toJSONString(record));
       logger.info("insertOrdercomment end");
       Integer i = orderCommentDao.insertOrdercomment(record);   
       ProductAssessVo proAs = new ProductAssessVo();
       proAs.setOrderId(record.getOrderId());
       proAs.setStatus(record.getComResult());
       proAs.setDelFlag("1");
       BaseResult baseresult = productAssessService.selectCommentById(record.getOrderId());
       @SuppressWarnings("unchecked")
	   List<String> list = (List<String>) baseresult.getData();
       ArrayList<String> arr = (ArrayList<String>) getNewList(list);
       StringBuffer sb = new StringBuffer();
       for(int j=0; j<arr.size(); j++){
    	   sb.append(arr.get(j));
    	   if(j!=arr.size()-1){
    		   sb.append(",");
    	   }
       }
       proAs.setSupplyProductId(sb.toString());
       productAssessService.addProAssess(proAs);
       return BaseResult.success(i);
    }
    public static  List<String> getNewList(List<String> li){
        List<String> list = new ArrayList<String>();
        for(int i=0; i<li.size(); i++){
            String str = li.get(i);
            if(!list.contains(str)){
                list.add(str);
            }
        }
        return list;
    }
		


}
